<template>
  <div class="app-container">
    <div class="bg-container">
      <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams">
        <el-form-item label="成员昵称" prop="nickname">
          <el-input
              v-model="queryParams.nickname"
              clearable
              placeholder="请输入成员昵称"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成员手机号" prop="mobile">
          <el-input
              v-model="queryParams.mobile"
              clearable
              placeholder="请输入成员手机号"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="成员邮箱" prop="email">
          <el-input
              v-model="queryParams.email"
              clearable
              placeholder="请输入成员邮箱"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              icon="Plus"
              plain
              type="primary"
              @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              icon="Message"
              plain
              type="success"
              @click="handleInvite"
          >邀请
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              icon="Download"
              plain
              type="warning"
              @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="teamUserList" :show-overflow-tooltip="true" :stripe="true"
                @selection-change="handleSelectionChange">
        <el-table-column v-if="false" align="center" type="selection" width="55"/>
        <el-table-column label="成员昵称" prop="nickname"/>
        <el-table-column label="成员手机号" prop="mobile"/>
        <el-table-column label="成员邮箱" prop="email"/>
        <el-table-column label="创建时间" prop="createTime">
          <template #default="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作"
                         width="240px">
          <template #default="scope">
            <el-button icon="View" link plain
                       type="primary" @click="handleView(scope.row)">查看
            </el-button>
            <el-button icon="Edit" link plain
                       type="primary" @click="handleUpdate(scope.row)">修改
            </el-button>
            <el-button v-if="false" icon="Delete" link plain
                       type="danger" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          v-model:current-page="queryParams.pageNo"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[3, 5, 7, 10]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; justify-content: flex-end"
          @size-change="getList"
          @current-change="getList"
      />
    </div>

    <!-- 添加或修改成员 -->
    <el-dialog v-model="open" :title="title" append-to-body width="600px">
      <el-form ref="teamUserRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="成员账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入成员账号"/>
        </el-form-item>
        <el-form-item label="成员昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入成员昵称"/>
        </el-form-item>
        <el-form-item label="成员手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入成员手机号"/>
        </el-form-item>
        <el-form-item label="成员邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入成员邮箱"/>
        </el-form-item>
        <el-form-item label="成员性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :value="0">男</el-radio>
            <el-radio :value="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="成员密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入成员密码" type="password"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看成员信息 -->
    <el-dialog v-model="show" append-to-body title="查看成员信息" width="600px">
      <el-descriptions border>
        <el-descriptions-item label="成员头像">
          <el-avatar :size="50" :src="form.avatar || '@/assets/imgs/square.png'" shape="square"/>
        </el-descriptions-item>
        <el-descriptions-item label="成员编号">
          {{ form.id || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员用户名">
          {{ form.username || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员昵称">
          {{ form.nickname || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="所在部门名称">
          {{ form.deptName || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员手机号">
          {{ form.mobile || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员邮箱">
          {{ form.email || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员备注">
          {{ form.remark || '--' }}
        </el-descriptions-item>
        <el-descriptions-item label="成员性别">
          <el-tag v-if="form.sex === 0" type="success">男</el-tag>
          <el-tag v-else-if="form.sex === 1">女</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ parseTime(form.createTime) || '--' }}
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelView">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改成员 -->
    <el-dialog v-model="invite" title="邀请成员" append-to-body width="600px">
      <el-form ref="teamUserRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="邀请成员" prop="email">
          <el-row :gutter="10">
            <el-col :span="24" class="mb8">
              <el-button type="primary" @click="form.email.push('')">新增</el-button>
            </el-col>
            <el-col v-for="(item, index) in form.email" :span="24" :index="index" class="mb8">
              <el-input v-model="form.email[index]" placeholder="请输入成员邮箱">
                <template #append>
                  <el-button type="danger" icon="Delete" @click="form.email.splice(index, 1)">删除
                  </el-button>
                </template>
              </el-input>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitInvite">确 定</el-button>
          <el-button @click="cancelInvite">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="TeamInfo" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {
  addTeamUser,
  delTeamUser,
  exportTeamUser,
  getTeamUser,
  inviteTeamUser,
  listTeamUser,
  updateTeamUser
} from "@/api/teamUser";

const {proxy} = getCurrentInstance();

const teamUserList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const show = ref(false)
const invite = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    nickname: '',
    mobile: '',
    email: '',
  },
  rules: {
    username: [
      {required: true, message: "成员账号不能为空", trigger: ["blur", "change"]}
    ],
    nickname: [
      {required: true, message: "成员昵称不能为空", trigger: ["blur", "change"]}
    ],
    mobile: [
      {required: true, message: "成员手机号不能为空", trigger: ["blur", "change"]}
    ],
    email: [
      {required: true, message: "成员邮箱不能为空", trigger: ["blur", "change"]}
    ],
    sex: [
      {required: true, message: "性别不能为空", trigger: ["blur", "change"]}
    ],
    password: [
      {required: true, message: "成员密码不能为空", trigger: ["blur", "change"]}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

// 查询团队成员列表
function getList() {
  loading.value = true;
  loading.value = true;
  listTeamUser(queryParams.value).then(response => {
    teamUserList.value = response.data.list;
    total.value = response.data.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    username: null,
    password: null,
    nickname: null,
    remark: null,
    deptId: null,
    postIds: null,
    email: null,
    mobile: null,
    sex: null,
    avatar: null,
    status: null,
    loginIp: null,
    loginDate: null,
    creator: null,
    createTime: null,
    updater: null,
    updateTime: null
  };
  proxy.resetForm("teamUserRef");
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 新增按钮操作
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加成员";
}

// 修改按钮操作
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value[0]
  getTeamUser({id: id}).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改成员";
  });
}

// 提交按钮
function submitForm() {
  proxy.$refs["teamUserRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTeamUser(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTeamUser(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

// 删除按钮操作
function handleDelete(row) {
  const idList = (row.id ? [row.id] : null) || ids.value;
  proxy.$modal.confirm('是否确认删除用户信息编号为"' + idList + '"的数据项？').then(function () {
    return delTeamUser({ids: idList.join(',')});
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

// 导出按钮操作
function handleExport() {
  exportTeamUser(queryParams.value)
}

// 查看成员操作
function handleView(row) {
  reset();
  const id = row.id || ids.value[0]
  getTeamUser({id: id}).then(response => {
    form.value = response.data;
    show.value = true;
  });
}

// 取消查看成员
function cancelView() {
  show.value = false;
  reset();
}

// 邀请成员
function handleInvite() {
  reset();
  form.value.email = [];
  form.value.email.push('')
  invite.value = true;
}

// 提交邀请
function submitInvite() {
  proxy.$refs["teamUserRef"].validate(valid => {
    if (valid) {
      if (form.value.email.filter(e => !e).length !== 0) {
        proxy.$modal.msgError("不能输入空邮箱，请补全");
        return
      }
      inviteTeamUser({email: form.value.email.join(',')}).then(res => {
        if (res.data) {
          proxy.$modal.msgSuccess("邀请成功");
          getList();
        }
      }).finally(() => {
        invite.value = false;
      })
    }
  });
}

// 取消邀请成员
function cancelInvite() {
  invite.value = false;
  reset();
}

getList();
</script>

<style lang="scss" scoped>
.el-select, .el-input {
  max-width: 300px !important;
}
</style>